﻿using System;
using System.Collections.Generic;
using System.Xml;

using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.packager.standardPackageActions;

namespace CreativeMinds.Umbraco.Installation.PackageActions {

	/// <summary>
	/// 
	/// </summary>
	public abstract class PackageAction {

		public abstract string Alias();

		public virtual XmlNode SampleXml() {
			return helper.parseStringToXmlNode(String.Format("<Action runat=\"install\" alias=\"{0}\" />", this.Alias()));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="packageName"></param>
		/// <param name="xmlData"></param>
		/// <returns></returns>
		public bool Execute(string packageName, XmlNode xmlData) {
			try {
				return this.Install(packageName, xmlData);
			}
			catch (Exception ex) {
				Log.Add(LogTypes.Error, -1, String.Format("{1}{0}{2}{0}{3}", Environment.NewLine, ex.TargetSite, ex.Message, ex.StackTrace));
				return false;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="packageName"></param>
		/// <param name="xmlData"></param>
		/// <returns></returns>
		public bool Undo(string packageName, XmlNode xmlData) {
			try {
				return this.Uninstall(packageName, xmlData);
			}
			catch (Exception ex) {
				Log.Add(LogTypes.Error, -1, String.Format("{1}{0}{2}{0}{3}", Environment.NewLine, ex.TargetSite, ex.Message, ex.StackTrace));
				return false;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected abstract bool Install(string packageName, XmlNode xmlData);
		/// <summary>
		/// 
		/// </summary>
		protected abstract bool Uninstall(string packageName, XmlNode xmlData);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="node"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		protected string GetAttributeValue(XmlNode node, string name) {
			return node.Attributes.GetNamedItem(name).Value;
		}
	}
}